using System.Diagnostics;
using System.IO;
using System;
using System.Xml.Serialization;
//using Xfinity.Debugging.Visualizers;

namespace Xfinity.Scripting
{
    /// <summary>
    /// A script to run.
    /// </summary>
//#if DEBUG
//    [DebuggerVisualizer(typeof(ScriptVisualizer))]
//#endif
    [Serializable]
    public class Script
    {
        public Script()
        {
            code = "";
        }
        /// <summary>
        /// Constructs a new Script object from a string.
        /// </summary>
        /// <param name="script">The script code.</param>
        /// <returns>A new script.</returns>
        [DebuggerStepThrough]
        public static Script FromString(string script)
        {
            Script s = new Script();
            s.code = script;
            ScriptManager.Scripts.Add(s);
            return s;
        }
        /// <summary>
        /// Constructs a new Script by reading from a file.
        /// </summary>
        /// <param name="filename">The filename to read.</param>
        /// <returns>A new script.</returns>
        [DebuggerStepThrough]
        public static Script FromFile(string filename)
        {
            Script s = new Script();
            s.code = File.ReadAllText(filename);
            ScriptManager.Scripts.Add(s);
            return s;
        }
        private string code;

        /// <summary>
        /// The script's source code.
        /// </summary>
        public string Code
        {
            [DebuggerStepThrough]
            get { return code; }
            [DebuggerStepThrough]
            set { code = value; }
        }
        /// <summary>
        /// Returns the source code from a Script.
        /// </summary>
        /// <param name="s">The script.</param>
        /// <returns></returns>
        [DebuggerStepThrough]
        public static implicit operator string(Script s)
        {
            return s.Code;
        }
        [DebuggerStepThrough]
        public static implicit operator Script(string s)
        {
            return FromString(s);
        }
        private Type compiledScript;
        private bool compiledScriptBeenSet;
        /// <summary>
        /// The compiled script.
        /// </summary>
        [XmlIgnore]
        public Type CompiledScript
        {
            [DebuggerStepThrough]
            get { return compiledScript; }
            [DebuggerStepThrough]
            set { if (!compiledScriptBeenSet) { compiledScript = value; compiledScriptBeenSet = true; } }
        }


    }
}
